package com.fengxing.ams.web.facade;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fengxing.ams.web.facade.intf.IWaitingFacade;
import com.fengxing.ams.web.facade.mouble.waitingarea.IWaitingDAO;
import com.fengxing.ams.web.facade.mouble.waitingarea.WaitingCarVO;

@Component
public class WaitingFacade implements IWaitingFacade {
	
	@Autowired
	IWaitingDAO waitingDAO;

	@Override
	public List<WaitingCarVO> findAll() {
		List<WaitingCarVO> cars = new ArrayList<WaitingCarVO>();
		 Iterator<WaitingCarVO> iter = this.waitingDAO.findAll().iterator();
		while (iter.hasNext()) {
			WaitingCarVO car = iter.next();
			long enterTime = car.getEnterTime().getTime();
			long now = System.currentTimeMillis();
			long timePass = now - enterTime;
			if (timePass <= 15 * 60 * 1000) {
				car.setDescTime("刚刚");
			} else if (timePass > 15 * 60 * 1000 && timePass <= 30 * 60 * 1000) {
				car.setDescTime("几分钟之前");
			} else if (timePass > 30 * 60 * 1000 && timePass <= 60 * 60 * 1000) {
				car.setDescTime("半个小时前");
			} else if (timePass > 60 * 60 * 1000 && timePass <= 2 * 60 * 60 * 1000) {
				car.setDescTime("一小时前");
			} else if (timePass > 2 * 60 * 60 * 1000 && timePass <= 6 * 60 * 60 * 1000){
				car.setDescTime("几小时前");
			} else if (timePass > 6 * 60 * 60 * 1000 && timePass <= 24 * 60 * 60 * 1000) {
				car.setDescTime("半天前");
			} else if (timePass > 24 * 60 * 60 * 1000 && timePass <= 2 * 24 * 60 * 60 * 1000) {
				car.setDescTime("一天前");
			} else {
				car.setDescTime("几天前");
			}
			
			car.setEncodeCarNumber(URLEncoder.encode(car.getCarNumber()));
			cars.add(car);
		}
		return cars;
	}

	@Override
	public boolean findOne(String id) {
		return this.waitingDAO.exists(id);
	}

	@Override
	public void save(String id) {
		WaitingCarVO entity = new WaitingCarVO();
		entity.setCarNumber(id);
		this.waitingDAO.save(entity);
	}

	@Override
	public void delete(String id) {
		this.waitingDAO.delete(id);
	}

}
